@ECHO OFF

REM RESTful DDS is a prototyping effort to demonstrate how DDS could be combined
REM with HTTP clients, extending the realm of pub/sub distribution to true wide
REM area networking.
REM
REM Copyright (C) 2009 PrismTech Ltd.
REM reinier.torenbeek@gmail.com
REM
REM This library is free software; you can redistribute it and/or
REM modify it under the terms of the GNU Lesser General Public
REM License Version 3 dated 29 June 2007, as published by the
REM Free Software Foundation.
REM
REM This library is distributed in the hope that it will be useful,
REM but WITHOUT ANY WARRANTY; without even the implied warranty of
REM MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
REM Lesser General Public License for more details.
REM
REM You should have received a copy of the GNU Lesser General Public
REM License along with restful-dds; if not, write to the Free Software
REM Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

REM Call batch file containing locations of external jar files.
REM Edit externals.bat if you want to point to the jars other than the
REM default locations.

SETLOCAL ENABLEDELAYEDEXPANSION

CALL externals.bat
SET EXTERNAL_JARS=%RESTLET_INTF_JAR%;%RESTLET_IMPL_JAR%;%GSON_JAR%

SET GEN_DIR=generated
SET OBJ_DIR=obj
SET LIB_DIR=lib
SET SRC_DIR=src

SET SERVICE_GEN_DIR=%GEN_DIR%
SET SERVICE_OBJ_DIR=%OBJ_DIR%
SET SERVICE_LIB_DIR=%LIB_DIR%

SET SERVICE_PKG=org.opensplice.restful.service
SET SERVICE_DIR=org\opensplice\restful\service
SET SERVICE_SOURCE_DIR=%SRC_DIR%\%SERVICE_DIR%
SET SERVICE_JAR=%SERVICE_LIB_DIR%\%SERVICE_PKG%.jar

SET CHAT_MODULE=Chat
SET CHAT_SOURCE_DIR=%SERVICE_GEN_DIR%\%CHAT_MODULE%

REM First generate code for built in test
REM This is not required for the service to work, but is left in here for
REM testing purposes.
ECHO Generating test classes using OpenSplice
IF NOT EXIST %SERVICE_GEN_DIR% MD %SERVICE_GEN_DIR%
idlpp -S -l java -d %SERVICE_GEN_DIR% "%CHAT_IDL%"

REM Compile generated source code
ECHO Compiling generated Tutorial source code
IF NOT EXIST %SERVICE_OBJ_DIR% MD %SERVICE_OBJ_DIR%
javac -sourcepath %SERVICE_GEN_DIR% -d %SERVICE_OBJ_DIR% -classpath "%SERVICE_OBJ_DIR%;%DDS_JAR%" %CHAT_SOURCE_DIR%\*.java

REM Compile service source code
IF NOT EXIST %SERVICE_OBJ_DIR% MD %SERVICE_OBJ_DIR%
ECHO Compiling RESTfulDDS service source code
javac -sourcepath %SERVICE_SOURCE_DIR% -d %SERVICE_OBJ_DIR% -classpath "%SERVICE_OBJ_DIR%;%DDS_JAR%;%EXTERNAL_JARS%" %SERVICE_SOURCE_DIR%\*.java

IF NOT EXIST %SERVICE_LIB_DIR% MD %SERVICE_LIB_DIR%
ECHO Creating RESTfulDDS service jar file
jar cf %SERVICE_JAR% -C %SERVICE_OBJ_DIR% %SERVICE_DIR%

REM Build the Chatroom Tutorial for testing purposes
IF EXIST Tutorial (
    ECHO Skipped building of chatroom tutorial because it seems to be there already
) else (
    ECHO Copying and building OpenSplice chatroom tutorial
    XCOPY /Q /S /I "%TUTORIAL_DIR%" Tutorial
    PUSHD Tutorial
    call BUILD.bat
    POPD
)


REM Build the Battleship demo
SET BATTLESHIP_NAME=battleship
SET BATTLESHIP_DIR=demos\%BATTLESHIP_NAME%
SET BATTLESHIP_OBJ_DIR=%BATTLESHIP_DIR%\%OBJ_DIR%
SET BATTLESHIP_LIB_DIR=%BATTLESHIP_DIR%\%LIB_DIR%
SET BATTLESHIP_SOURCE_DIR=%BATTLESHIP_DIR%\%SRC_DIR%
SET BATTLESHIP_JAR=%BATTLESHIP_LIB_DIR%\%BATTLESHIP_NAME%.jar

SET TRACK_IDL=%BATTLESHIP_DIR%\idl\track.idl
SET TRACK_MODULE=track
SET TRACK_GEN_DIR=%BATTLESHIP_DIR%\%GEN_DIR%
SET TRACK_SOURCE_DIR=%TRACK_GEN_DIR%\%TRACK_MODULE%
SET TRACK_OBJ_DIR=%BATTLESHIP_OBJ_DIR%\%GEN_DIR%
SET TRACK_JAR=%BATTLESHIP_LIB_DIR%\%TRACK_MODULE%.jar

ECHO Generating typed classes for Battleship demo
IF NOT EXIST %TRACK_GEN_DIR% MD %TRACK_GEN_DIR%
idlpp -S -l java -d %TRACK_GEN_DIR% "%TRACK_IDL%"

ECHO Compiling generated Battleship code
IF NOT EXIST %TRACK_OBJ_DIR% MD %TRACK_OBJ_DIR%
javac -sourcepath %TRACK_GEN_DIR% -d %TRACK_OBJ_DIR% -classpath "%TRACK_OBJ_DIR%;%DDS_JAR%" %TRACK_SOURCE_DIR%\*.java

ECHO Compiling Battleship source files
SET BATTLESHIP_PACKAGES=track Battlefield Display Identification Sensor
FOR %%f IN (%BATTLESHIP_PACKAGES%) DO javac -d %BATTLESHIP_OBJ_DIR% -classpath "%BATTLESHIP_OBJ_DIR%;%TRACK_OBJ_DIR%;%DDS_JAR%" %BATTLESHIP_SOURCE_DIR%\%%f\*.java

ECHO Creating Battleship jar file
SET JAR_PARAMS=
FOR %%f IN (%BATTLESHIP_PACKAGES%) DO SET JAR_PARAMS=!JAR_PARAMS! -C %BATTLESHIP_OBJ_DIR% %%f
IF NOT EXIST %BATTLESHIP_LIB_DIR% MD %BATTLESHIP_LIB_DIR%
jar cf %BATTLESHIP_JAR% %JAR_PARAMS%

ECHO Creating track jar file
IF NOT EXIST %BATTLESHIP_LIB_DIR% MD %BATTLESHIP_LIB_DIR%
jar cf %TRACK_JAR% -C %TRACK_OBJ_DIR% %TRACK_MODULE%

REM Only compile web application if the GWT_HOME environment variable has been
Rem set. Otherwise, skip this step.
IF "_%GWT_HOME%"==_ GOTO NoGWT

SET WEBAPP_PKG=DisplayWebApp
SET WEBAPP_ENTRY_POINT=display.%WEBAPP_PKG%
SET WEBAPP_SUBDIR=display\client
SET WEBAPP_DIR=%BATTLESHIP_SOURCE_DIR%\%WEBAPP_PKG%
SET WEBAPP_SOURCE_DIR=%WEBAPP_DIR%\%SRC_DIR%
SET WEBAPP_DISPLAY_SOURCE_DIR=%WEBAPP_SOURCE_DIR%\%WEBAPP_SUBDIR%
SET WEBAPP_OBJ_DIR=%BATTLESHIP_OBJ_DIR%
SET WEBAPP_JAR=%BATTLESHIP_LIB_DIR%\webdisplay.jar
SET WEBAPP_WWW_DIR=%BATTLESHIP_DIR%\www
SET GWT_USER_JAR=%GWT_HOME%\gwt-user.jar
SET GWT_MAPS_JAR=external\gwt-maps.jar
SET GWT_DEV_JAR=%GWT_HOME%\gwt-dev.jar

ECHO Compiling Battlefield web display java source
IF NOT EXIST %WEBAPP_OBJ_DIR% MD %WEBAPP_OBJ_DIR%
javac -sourcepath %WEBAPP_SOURCE_DIR% -d %WEBAPP_OBJ_DIR% -classpath "%TRACK_OBJ_DIR%;%GWT_USER_JAR%;%GWT_MAPS_JAR%" %WEBAPP_DISPLAY_SOURCE_DIR%\*.java

ECHO Creating Battlefield web display jar
jar cf %WEBAPP_JAR% -C %WEBAPP_OBJ_DIR% %WEBAPP_SUBDIR%

ECHO GWT-compiling Java source code into java script
IF NOT EXIST %WEBAPP_WWW_DIR% MD %WEBAPP_WWW_DIR%
java -Xmx256M -classpath "%WEBAPP_SOURCE_DIR%;%WEBAPP_JAR%;%GWT_USER_JAR%;%GWT_MAPS_JAR%;%GWT_DEV_JAR%" com.google.gwt.dev.Compiler %WEBAPP_ENTRY_POINT% -war %WEBAPP_WWW_DIR%
REM java -Xmx256M -classpath "%WEBAPP_SOURCE_DIR%;%WEBAPP_JAR%;%GWT_USER_JAR%;%GWT_MAPS_JAR%;%GWT_DEV_JAR%" com.google.gwt.dev.DevMode %WEBAPP_ENTRY_POINT% -war %WEBAPP_WWW_DIR%

GOTO End

:NoGWT
ECHO Environment variable GWT_HOME not set, skipping web display compilation

:End